<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  lang="zh">
<head>
    <meta charset="UTF-8">
    <title>javaRun</title>
</head>
<script type="text/javascript" th:src="@{/static/js/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/static/bootstrap-3.4.1-dist/js/bootstrap.min.js}"></script>
<script type="text/javascript" th:src="@{/static/layer-v3.5.1/layer/layer.js}"></script>
<script type="text/javascript" th:src="@{/static/monaco-editor/vs/loader.js}"></script>
<script src="https://s0.pstatp.com/cdn/expire-1-M/ionicons/4.5.6/ionicons.js"></script>
<link rel="stylesheet" type="text/css" th:href="@{/static/bootstrap-3.4.1-dist/css/bootstrap.css}">
<link rel="stylesheet" type="text/css" th:href="@{/static/icheck-bootstrap/icheck-bootstrap.min.css}">
<link rel="stylesheet" type="text/css" th:href="@{/static/css/mystyle.css}">
<link rel="shortcut icon" th:href="@{/static/img/favicon.ico}">

<body>
<form role="form">
    <div class="form-group d-flex" style="padding: 0px 0px 0px 0px">
        <div style="width: 66.66666667%;">
<!--            <div id="container" style="height: calc(100% - 0px); width: calc(100% - 0px);"></div>-->
            <div id="parent-container" style="height: calc(90vh);"><!-- 800 650 -->
                <div id="container" style="height: 100%;">
                </div>
            </div>
        </div>
        <div style="width: 33.33333333%; padding: 0 11px;">
            <div style="padding: 20px 0 0 0" class="form-group clearfix">
                <div class="icheck-success float-left">
                    <input type="checkbox" id="timeLimitCheckBox" value="timeLimit" onclick="timeLimitClick()" name="timeLimitCheckBox">
                    <label for="timeLimitCheckBox" style="user-select: none;">超时（ms）</label>
                </div>
                <input type="text" id="timeLimitInput" style="display: none;" class="form-control"
                       placeholder="默认10000，范围0-10000">
                <div class="icheck-success float-left">
                    <input type="checkbox" id="argsCheckBox" onclick="argsCheckBoxClick()" name="argsCheckBox">
                    <label for="argsCheckBox" style="user-select: none;">参数，空格隔开</label>
                </div>
                <input type="text" id="argsInput" style="display: none;" class="form-control"
                       placeholder="">
                <button onclick="doSubmit()" id="submitButton" type="button" style="width: 100%;user-select: none;margin-top: 10px;outline: none;" class="btn btn-success" aria-hidden="true">RUN</button>

            </div>
            <p></p>
            <div style="">
                <div class="panel panel-info">
                    <div class="panel-heading">
                        <h3 class="panel-title">运行信息</h3>
                    </div>
                    <div class="" id="compileInfoDiv" style="word-spacing: 2.1px;white-space: break-spaces;margin:15px;">运行耗时：<br>编译状态：</div>
                </div>
            </div>

            <div style="">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title">运行结果</h3>
                    </div>
                    <div class=""  style="min-height: 250px;margin: 15px">
                        <textarea id="resultDiv" style="width: 100%; border-radius: 0; border: 0;outline: none; min-height: 240px; resize:vertical;"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>

</form>
<div id="footer">

    <b id="nav-site">JAVA 8 程序在线运行环境</b>
    <a class="site" id="git-branch-a" target="_black" href="https://github.com/rawchen/JavaRun" title="GitHub">
        <ion-icon id="git-branch" name="git-branch" class="logo"></ion-icon>
    </a>
</div>
</body>
<script>
    var myEditor;
    // require.config({ paths: { 'vs': '../static/monaco-editor/vs' }});
    require.config({ paths: { 'vs': '//cdn.rawchen.com/vs' }});
    require(['vs/editor/editor.main'], function() {
        //判断localStorage是否有之前的代码缓存，有就从那取，没有就使用定义好的helloworld。
        var code = 'class Main {\n' +
                '\tpublic static void main(String[] args) {\n' +
                '\t\tSystem.out.println("hello world!");\n' +
                '\t}\n' +
                '}';
        if (window.localStorage.getItem('CODE_RESOURCE') != null) {
            code = window.localStorage.getItem('CODE_RESOURCE');
        } else {
           // alert("NULL AND SET ORIGIN");
        }

        myEditor = monaco.editor.create(document.getElementById('container'), {
            value: [
                code
            ].join('\n'),
            language: 'java',
            theme: 'vs-dark',
            fontSize: 16,
            scrollBeyondLastLine: false,
            quickSuggestions: true,
            autoClosingBrackets: true,
            automaticLayout: true

        });

        // 创建代码提醒
        monaco.languages.registerCompletionItemProvider('java', {
            provideCompletionItems() {
                return {
                    suggestions: [
                        {
                            label: 'System.out.println();',
                            kind: monaco.languages.CompletionItemKind['Function'], //这里Function也可以是别的值，主要用来显示不同的图标
                            insertText: 'System.out.println($0);', // 我试了一下，如果没有此项，则无法插入
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                            // detail: ''
                        },
                        {
                            label: 'if',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'if (${1:condition}) {\n' +
                                    '\t$0\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'if else',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'if (${1:condition}) {\n' +
                                    '\t$0\n' +
                                    '} else {\n' +
                                    '\t\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'if elseif else',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'if (${1:condition}) {\n' +
                                    '\t$0\n' +
                                    '} else if (${2:condition}) {\n' +
                                    '\t\n' +
                                    '} else {\n' +
                                    '\t\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'List<> list = new ArrayList<>();',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'List<${1:String}> list = new ArrayList<>();',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet

                        },
                        {
                            label: 'Map<,> map = new HashMap<>();',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'Map<${1:String}, ${2:String}> map = new HashMap<>();',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet

                        },
                        {
                            label: 'Set<> set = new HashSet<>();',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'Set<${1:String}> set = new HashSet<>();',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet

                        },
                        {
                            label: 'Scanner sc = new Scanner(System.in);',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'Scanner sc = new Scanner(System.in);'

                        },
                        {
                            label: 'for (int i = 0; i < x; i++){}',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'for (int i = ${1:}; i < ${2:}; i++) {\n' +
                                    '\t$0\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                            detail: '创建迭代循环'

                        },
                        {
                            label: 'for (Object o : o){}',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'for (${1:Object} o : o) {\n' +
                                    '\t$0\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                            detail: 'for (T item : expr)'

                        },
                        {
                            label: 'valueOf()',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'valueOf($0)',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,

                        },
                        {
                            label: 'parseInt()',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'parseInt($0)',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,

                        },
                        {
                            label: 'toString()',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'toString()'

                        },
                        {
                            label: 'throw new Exception()',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'throw new Exception($0);',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet

                        },
                        {
                            label: 'throws Exception',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'throws Exception'

                        },
                        {
                            label: 'try / catch',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'try {\n' +
                                    '\t$0\n' +
                                    '} catch (Exception e) {\n' +
                                    '    System.out.println(e);\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet

                        },
                        {
                            label: 'try / catch / finally',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'try {\n' +
                                    '\t$0\n' +
                                    '} catch (Exception e) {\n' +
                                    '    System.out.println(e);\n' +
                                    '} finally {\n' +
                                    '\t\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet

                        },
                        {
                            label: 'while(condition){}',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'while (${1:condition}) {\n' +
                                    '\t$0\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'do while',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'do {\n' +
                                    '\t$0\n' +
                                    '} while (${1:condition});',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        },
                        {
                            label: 'import java.util.*;',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'import java.util.*;'
                        },
                        {
                            label: 'switch case',
                            kind: monaco.languages.CompletionItemKind['Function'],
                            insertText: 'switch (${1:condition}) {\n' +
                                    '\tcase ${2:value}:\n' +
                                    '\t\t$0\n' +
                                    '\t\tbreak;\n' +
                                    '\tdefault:\n' +
                                    '\t\t\n' +
                                    '}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
                        }
                    ]
                };
            },
            triggerCharacters: ['sout','if','new','for','.','th','tr','wh','do','im','sw']  // 写触发提示的字符，可以有多个
        });
    });

    function doSubmit() {
        var url = "/compileAndRun";
        // var url = "/test";
        var executeTimeLimit = $("#timeLimitInput").val().trim();
        var executeArgs = $("#argsInput").val().trim();
        if ($("#timeLimitCheckBox").prop("checked")) {
            if (executeTimeLimit == "") {
                executeTimeLimit = 1000;
            }
        } else {
            executeTimeLimit = null;
        }
        if ($("#argsCheckBox").prop("checked")) {
            if (executeArgs == "") {
                executeArgs = null;
            }
        } else {
            executeArgs = null;
        }
        window.localStorage.setItem('CODE_RESOURCE',myEditor.getValue());
        var data = {"javaSource": myEditor.getValue(), "executeTimeLimit": executeTimeLimit,"executeArgs": executeArgs};
        $("#submitButton").attr('disabled', true);
        $("#submitButton").text("Loading...");
        $.post(url, data, function (result) {
            // layer.msg("" + result.message);
            $("#submitButton").attr('disabled', false);
            $("#submitButton").text("RUN");
            //设置执行信息
            $("#compileInfoDiv").html("运行耗时：" + result.executeDurationTime + " ms<br>编译状态：" + result.message);
            //设置执行结果
            $("#resultDiv").html(result.executeResult);
        });
    }

    function timeLimitClick() {
        if ($("#timeLimitCheckBox").prop("checked")) {
            $("#timeLimitInput").show();
        } else {
            $("#timeLimitInput").hide();
        }
    }

    function argsCheckBoxClick() {
        if ($("#argsCheckBox").prop("checked")) {
            $("#argsInput").show();
        } else {
            $("#argsInput").hide();
        }
    }
</script>
</html>